{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c1efbfb1-f49a-4f5a-beed-fa489fa9af79",
   "metadata": {},
   "source": [
    "#### Create a temporary directory to hold a local package."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "719b375f-c388-434e-8f1c-857c129ca6dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "%goflags=[\"--cover\" \"--covermode=set\"]\n"
     ]
    }
   ],
   "source": [
    "%goflags --cover --covermode=set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ce78b904-9a4b-4d69-ae09-1dd499af397d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/tmp/gonb_tests_gowork_qBusQfXt"
     ]
    }
   ],
   "source": [
    "!*TMP_PKG=$(mktemp -d /tmp/gonb_tests_gowork_XXXXXXXX) \\\n",
    "  && echo -n \"${TMP_PKG}\" > TMP_PKG.txt\n",
    "!*cat TMP_PKG.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04fe8b6b-8f51-45b4-990a-cb749122d3c2",
   "metadata": {},
   "source": [
    "#### Create dummy Go module"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f648bc23-84bc-422d-a248-3217c61d4577",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temporary test package: /tmp/gonb_tests_gowork_qBusQfXt\n"
     ]
    }
   ],
   "source": [
    "func Assert(err error) {\n",
    "    if err != nil {\n",
    "        panic(err)\n",
    "    }\n",
    "}\n",
    "\n",
    "func Must[T any](v T, err error) T {\n",
    "    Assert(err)\n",
    "    return v\n",
    "}\n",
    "\n",
    "var TmpPkg string\n",
    "\n",
    "func init_TmpPkg() {\n",
    "    fPath := path.Join(os.Getenv(\"GONB_TMP_DIR\"), \"TMP_PKG.txt\")\n",
    "    TmpPkg = string(Must(os.ReadFile(fPath)))\n",
    "}\n",
    "\n",
    "%%\n",
    "fmt.Printf(\"Temporary test package: %s\\n\", TmpPkg)\n",
    "\n",
    "Assert(os.WriteFile(path.Join(TmpPkg, \"go.mod\"), []byte(\"module a.com/a/pkg\\n\\ngo 1.21\\n\"),0660))\n",
    "Assert(os.WriteFile(path.Join(TmpPkg, \"pkg.go\"), []byte(\n",
    "    \"package pkg\\n\\nfunc A() string { return \\\"A\\\" }\\n\"),0660))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d0ad4ac5-542c-4d51-90bb-aacdebd7b739",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "module a.com/a/pkg\n",
      "\n",
      "go 1.21\n",
      "package pkg\n",
      "\n",
      "func A() string { return \"A\" }\n"
     ]
    }
   ],
   "source": [
    "!*cat \"$(cat TMP_PKG.txt)\"/*"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a48338b-28e0-44b0-ab54-aea4b2ad6630",
   "metadata": {},
   "source": [
    "#### Test creating `go.work` and using `%goworkfix`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9686f8da-7eec-46cc-9e86-857f09046142",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t- Added replace rule for module \"a.com/a/pkg\" to local directory \"/tmp/gonb_tests_gowork_qBusQfXt\".\n"
     ]
    }
   ],
   "source": [
    "!*rm -f go.work && go work init && go work use . \"$(cat TMP_PKG.txt)\"\n",
    "%goworkfix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4be74e09-79ee-4543-8fc0-176d2739e253",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "module gonb_63ffc329\n",
      "\n",
      "go 1.25.3\n",
      "\n",
      "replace a.com/a/pkg => TMP_PKG\n"
     ]
    }
   ],
   "source": [
    "!*cat go.mod | sed 's|'\"$(cat TMP_PKG.txt)\"'|TMP_PKG|g;'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a62127c7-21e1-4cdb-a1b1-70b1932c394d",
   "metadata": {},
   "source": [
    "#### Check for tracked directories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9663da2f-8bb0-4a75-be87-4188d388817e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<b>List of files/directories being tracked:</b>\n",
       "<ul>\n",
       "<li>/tmp/gonb_tests_gowork_qBusQfXt</li>\n",
       "</ul>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%track"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "53749a40-0c3c-42c0-ad06-16ec6bbbe218",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "module gonb_63ffc329\n",
      "\n",
      "go 1.25.3\n"
     ]
    }
   ],
   "source": [
    "!*rm -f go.work\n",
    "!*go mod edit -dropreplace=a.com/a/pkg\n",
    "!*cat go.mod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "57095c0a-9a48-4925-873d-a52cf1b94174",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tUntracked \"/tmp/gonb_tests_gowork_...\"\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<b>No files or directory being tracked yet</b>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%untrack /tmp/gonb_tests_gowork_...\n",
    "%track"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ae85ba5-bd5c-467a-ac88-734591ffd597",
   "metadata": {},
   "source": [
    "#### Clean up temporary directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fb455339-1f95-4b55-a243-c53861642095",
   "metadata": {},
   "outputs": [],
   "source": [
    "!*rm -r \"$(cat TMP_PKG.txt)\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Go (gonb)",
   "language": "go",
   "name": "gonb"
  },
  "language_info": {
   "codemirror_mode": "",
   "file_extension": ".go",
   "mimetype": "text/x-go",
   "name": "go",
   "nbconvert_exporter": "",
   "pygments_lexer": "",
   "version": "go1.25.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
